﻿'*******************************************************************
' Program: Data Center Visio Rack Tool
' Author: Albert E Edlund
' Date:
'
' Purpose:
'
'
'*******************************************************************

Imports Microsoft.Practices.EnterpriseLibrary.Common
Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging


Imports System.Collections.ObjectModel


Imports System.Data
Imports System.Array
Imports System.Data.Common
Imports Microsoft.SqlServer
Imports Microsoft.SqlServer.Types
Imports System.Xml
Imports System.IO


Imports DC_Rack.Objects.clsDC_RackData

Public Class clsCompHierarchy


#Region "properties and definitions"

    Public m_db As Database
    Public m_ds As DataSet
    Public m_dt As DataTable
    Private strUserId As String

    Public m_deleteCommand As DbCommand
    Public m_insertCommand As DbCommand
    Public m_updateCommand As DbCommand



    Public Const readAll As String = "selectTblCompHierarchy"


    Private m_chcomppath As Microsoft.SqlServer.Types.SqlHierarchyId
    Public Property chCompPath() As Microsoft.SqlServer.Types.SqlHierarchyId
        Get
            Return Me.m_chcomppath
        End Get
        Set(ByVal value As Microsoft.SqlServer.Types.SqlHierarchyId)
            m_chcomppath = value
        End Set
    End Property

    Private m_chcompkey As Int32
    Public Property chCompKey() As Int32
        Get
            Return Me.m_chcompkey
        End Get
        Set(ByVal value As Int32)
            Me.m_chcompkey = value
        End Set
    End Property

    ' this is built in the database based on the data in the
    ' chcomp field, we don't build one here
    Private m_chparentcomppath As Microsoft.SqlServer.Types.SqlHierarchyId
    Public Property chCompParentPath() As Microsoft.SqlServer.Types.SqlHierarchyId
        Get
            Return Me.m_chparentcomppath
        End Get
        Set(ByVal value As Microsoft.SqlServer.Types.SqlHierarchyId)
            Me.m_chparentcomppath = value
        End Set
    End Property

    Private m_chcomproot As Int32
    Public Property chCompRoot() As Int32
        Get
            Return Me.m_chcomproot
        End Get
        Set(ByVal value As Int32)
            Me.m_chcomproot = value
        End Set
    End Property

    Private m_chcomplastupdate As DateTime
    Public Property chCompLastUpdate() As DateTime
        Get
            Return Me.m_chcomplastupdate
        End Get
        Set(ByVal value As DateTime)
            m_chcomplastupdate = value
        End Set
    End Property

    Private m_chcompname As String
    Public Property chCompName() As String
        Get
            chCompName = m_chcompname
        End Get
        Set(ByVal value As String)
            m_chcompname = value
        End Set
    End Property

    Private m_chcomplevel As Int16
    Public Property chCompLevel() As Int16
        Get
            Return Me.m_chcomplevel
        End Get
        Set(ByVal value As Int16)
            m_chcomplevel = value
        End Set
    End Property

    '
    ' for reading records
    '
    Public Sub moveDataRowToProperties(ByRef tempRow As DataRow)

        Try

            chCompPath = tempRow.Item("chcomppath")
            chCompKey = tempRow.Item("chcompkey")
            chCompName = tempRow.Item("chcompname")
            chCompParentPath = tempRow.Item("chcompparentpath")
            chCompRoot = tempRow.Item("chcomproot")
            chCompLastUpdate = tempRow.Item("chcomplastupdate")
            chCompLevel = tempRow.Item("chcomplevel")

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub


    Public Sub initDataSetAndTable()

        Try

            ' make sure the root stuff is there
            If m_ds Is Nothing Then
                m_ds = New DataSet
            Else
                m_ds = Nothing
                m_ds = New DataSet
            End If
            m_ds.SchemaSerializationMode = SchemaSerializationMode.IncludeSchema

            If m_dt Is Nothing Then
                m_dt = New DataTable
            Else
                m_dt = Nothing
                m_dt = New DataTable
            End If


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try



    End Sub


#End Region

#Region "Database Calls"

    Public Sub dbSelectAllCompHierarchy()



        Try

            initDataSetAndTable()

            ' Retrieve the initial data
            Dim cmd As DbCommand = m_db.GetStoredProcCommand(readAll)
            m_db.LoadDataSet(cmd, m_ds, DC_RackTables.tblCompHierarchy.ToString)

            m_dt = m_ds.Tables(DC_RackTables.tblCompHierarchy.ToString)


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    ' given a component key get a list of descendants from
    ' comphierarchy
    '
    Public Function dbSelectComponentsByRootKey(ByVal intCompKey As Int32) _
            As System.Data.DataTable


        Try

            initDataSetAndTable()

            Dim cmd As DbCommand = m_db.GetStoredProcCommand("selectComponentsByRootKey")
            m_db.AddInParameter(cmd, "@rootKey", DbType.Int32, intCompKey)
            m_db.LoadDataSet(cmd, m_ds, DC_RackTables.tblCompHierarchy.ToString)
            m_dt = m_ds.Tables(DC_RackTables.tblCompHierarchy.ToString)


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

        dbSelectComponentsByRootKey = m_dt

    End Function


    Public Sub dbSelectComponentsCompKey(ByVal intCompKey As Int32)


        Try

            initDataSetAndTable()

            Dim cmd As DbCommand = m_db.GetStoredProcCommand("selectCompHierarchyCompKey")
            m_db.AddInParameter(cmd, "@chcompKey", DbType.Int32, intCompKey)
            m_db.LoadDataSet(cmd, m_ds, DC_RackTables.tblCompHierarchy.ToString)
            m_dt = m_ds.Tables(DC_RackTables.tblCompHierarchy.ToString)


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try


    End Sub


    '
    ' pass in the string version of the comphierarchy, compkey, compname,
    ' the rootkey, and the number of operands
    '
    '
    Public Function dbInsertCompHierarchy _
        (ByVal strCompPath As String, _
        ByVal intCompKey As Int32, _
        ByVal strCompName As String, _
        ByVal intCompRoot As Int32, _
        ByVal intCompLevel As Int16) _
        As Int16

        Dim intRows As Integer = 0


        Try

            Dim cmd As DbCommand = m_db.GetStoredProcCommand("insertCompHierarchy")
            m_db.AddInParameter(cmd, "@strHierarchy", DbType.String, strCompPath)
            m_db.AddInParameter(cmd, "@compKey", DbType.Int32, intCompKey)
            m_db.AddInParameter(cmd, "@compName", DbType.String, strCompName)
            m_db.AddInParameter(cmd, "@compRoot", DbType.Int16, intCompRoot)
            m_db.AddInParameter(cmd, "@compLevel", DbType.Int16, intCompLevel)
            intRows = m_db.ExecuteNonQuery(cmd)

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

        dbInsertCompHierarchy = introws

    End Function

    Public Function dbDeleteCompHierarchy _
            (ByVal intCompKey As Int32) _
            As Int16

        Dim intReturn As Int16 = 0

        Try

            Dim cmd As DbCommand
            cmd = m_db.GetStoredProcCommand("deleteCompHierarchy")
            m_db.AddInParameter(cmd, "@compKey", DbType.Int32, intCompKey)

            intReturn = m_db.ExecuteNonQuery(cmd)


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

        dbDeleteCompHierarchy = intReturn


    End Function


#End Region

#Region "Build Component Hierarchy"

    '
    '
    '
    Public Sub buildCompHierarchy()

        Dim m_sql As String = ""

        Dim m_db As Database
        Dim m_ds As DataSet = Nothing
        Dim m_dt As DataTable = Nothing

        Dim intCmd As Integer

        Try
            m_db = DatabaseFactory.CreateDatabase()
            Dim cmd As DbCommand
            Dim strNow As Date = Now

            intCmd = 1
            ' dropconstraintFkCompHierarchyParent
            cmd = m_db.GetStoredProcCommand("dropconstraintFkCompHierarchyParent")
            m_db.ExecuteNonQuery(cmd)

            intCmd = 2
            'droptableCompHierarchy
            cmd = m_db.GetStoredProcCommand("droptableCompHierarchy")
            m_db.ExecuteNonQuery(cmd)

            Try
                'createtableCompHierarchy
                intCmd = 3
                cmd = m_db.GetStoredProcCommand("createtableCompHierarchy")
                m_db.ExecuteNonQuery(cmd)
            Catch
                MsgBox("create table error")
            End Try

            intCmd = 4
            'addconstraintPkCompHierarchy
            cmd = m_db.GetStoredProcCommand("addconstraintPkCompHierarchy")
            m_db.ExecuteNonQuery(cmd)

            intCmd = 5

            'addconstraintfkCompHierarchyParent
            cmd = m_db.GetStoredProcCommand("addconstraintfkCompHierarchyParent")
            m_db.ExecuteNonQuery(cmd)


            ' addCompHierarchyRoot
            intCmd = 6
            cmd = m_db.GetStoredProcCommand("addCompHierarchyRoot")
            m_db.ExecuteNonQuery(cmd)

            ' now create the data
            Try
                ' buildCompHierarchyData
                intCmd = 7
                cmd = m_db.GetStoredProcCommand("buildCompHierarchyData")
                m_db.ExecuteNonQuery(cmd)
            Catch
                MsgBox("error on create data")
            End Try

            'updateCompHierarchyHelpers
            ' get a level count and the root key
            cmd = m_db.GetStoredProcCommand("updateCompHierarchyHelpers")
            m_db.ExecuteNonQuery(cmd)


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub


#End Region


    Public Sub New()
        Try



            m_db = DatabaseFactory.CreateDatabase()

            m_ds = New DataSet
            m_dt = New DataTable

            m_updateCommand = Nothing
            m_insertCommand = Nothing
            m_deleteCommand = Nothing

            strUserId = Environment.UserDomainName & "/" & Environment.UserName

        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub







End Class
